package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.model.MarketCategory;
import com.cskaoyan.model.MarketCategoryExample;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/8
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MarketCategoryMapper categoryMapper;

    @Override
    public List<MarketCategory> list(String level, int page, int limit) {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andLevelEqualTo(level)
                .andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(page,limit);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        return marketCategories;
    }

    @Override
    public MarketCategory findById(Integer categoryId) {
        MarketCategory category = categoryMapper.selectByPrimaryKey(categoryId);
        return category;
    }

    @Override
    public List<MarketCategory> queryChildren(Integer categoryId) {
        MarketCategoryExample example = new MarketCategoryExample();
        example.setOrderByClause("add_time desc");
        example.createCriteria().andDeletedEqualTo(false)
                .andPidEqualTo(categoryId);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        return marketCategories;
    }

    @Override
    public List queryLevelOne() {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andLevelEqualTo("L1");
        example.setOrderByClause("add_time desc");
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        List<Map<Object, Object>> list = marketCategories.stream().map(marketCategory -> {
            Map<Object, Object> map = new HashMap<>();
            map.put("iconUrl", marketCategory.getIconUrl());
            map.put("id", marketCategory.getId());
            map.put("name", marketCategory.getName());
            return map;
        }).collect(Collectors.toList());
        return list;
    }
}
